﻿using System;
using System.Collections.Generic;
using Fizbin.Kinect.Gestures.Segments;
using Microsoft.Kinect;
using System.Windows;
using System.Windows.Forms;
using System.Threading;

namespace Fizbin.Kinect.Gestures
{
    public class GestureController
    {
        /// <summary>
        /// The list of all gestures we are currently looking for
        /// </summary>
        private List<Gesture> gestures = new List<Gesture>();
        public System.Media.SoundPlayer player;
        private TextBox tex = new TextBox(); 

        /// <summary>
        /// Initializes a new instance of the <see cref="GestureController"/> class.
        /// </summary>
        public GestureController()
        {

            player = new System.Media.SoundPlayer(@"HadoukenTheme.wav");

            //CreateMyForm();

            // Define the gestures

            //IRelativeGestureSegment[] joinedhandsSegments = new IRelativeGestureSegment[20];
            JoinedHandsSegment1 joinedhandsSegment = new JoinedHandsSegment1(tex);
            //for (int i = 0; i < 20; i++)
            //{
            //    // gesture consists of the same thing 10 times 
            //    joinedhandsSegments[i] = joinedhandsSegment;
            //}
            //AddGesture(GestureType.JoinedHands, joinedhandsSegments);

            IRelativeGestureSegment[] menuSegments = new IRelativeGestureSegment[20];
            MenuSegment1 menuSegment = new MenuSegment1();
            for (int i = 0; i < 20; i++)
            {
                // gesture consists of the same thing 20 times 
                menuSegments[i] = menuSegment;
            }
            AddGesture(GestureType.Menu, menuSegments);

            //New Hadouken
            IRelativeGestureSegment[] hadoukenSegments = new IRelativeGestureSegment[6];

            hadoukenSegments[0] = new HadoukenSegment1(tex, player);
            hadoukenSegments[1] = new HadoukenSegment2(tex);
            hadoukenSegments[2] = new HadoukenSegment1(tex, player);
            hadoukenSegments[3] = new HadoukenSegment2(tex);
            hadoukenSegments[4] = new HadoukenSegment3(tex);
            hadoukenSegments[5] = new HadoukenSegment4(tex);

            //hadoukenSegments[5] = new HadoukenSegment1(tex);

            //for (int i = 21; i < 41; i++)
            //{
            //    // gesture consists of the same thing 10 times 
            //    hadoukenSegments[i] = joinedhandsSegment;
            //}

            //hadoukenSegments[41] = new HadoukenSegment2(tex);

            AddGesture(GestureType.Hadouken, hadoukenSegments);

            //Hadouken
            //IRelativeGestureSegment[] hadoukenSegments = new IRelativeGestureSegment[2];
            //hadoukenSegments[0] = new HadoukenSegment1(tex);
            ////hadoukenSegments[1] = new HadoukenSegment2(tex);
            //hadoukenSegments[1] = new HadoukenSegment3(tex);
            //AddGesture(GestureType.Hadouken, hadoukenSegments);

            IRelativeGestureSegment[] swiperightSegments = new IRelativeGestureSegment[3];
            swiperightSegments[0] = new SwipeRightSegment1(tex);
            swiperightSegments[1] = new SwipeRightSegment2(tex);
            swiperightSegments[2] = new SwipeRightSegment3(tex);
            AddGesture(GestureType.SwipeRight, swiperightSegments);

            //IRelativeGestureSegment[] waveRightSegments = new IRelativeGestureSegment[6];
            //WaveRightSegment1 waveRightSegment1 = new WaveRightSegment1();
            //WaveRightSegment2 waveRightSegment2 = new WaveRightSegment2();
            //waveRightSegments[0] = waveRightSegment1;
            //waveRightSegments[1] = waveRightSegment2;
            //waveRightSegments[2] = waveRightSegment1;
            //waveRightSegments[3] = waveRightSegment2;
            //waveRightSegments[4] = waveRightSegment1;
            //waveRightSegments[5] = waveRightSegment2;
            //AddGesture(GestureType.WaveRight, waveRightSegments);

            //IRelativeGestureSegment[] waveLeftSegments = new IRelativeGestureSegment[6];
            //WaveLeftSegment1 waveLeftSegment1 = new WaveLeftSegment1();
            //WaveLeftSegment2 waveLeftSegment2 = new WaveLeftSegment2();
            //waveLeftSegments[0] = waveLeftSegment1;
            //waveLeftSegments[1] = waveLeftSegment2;
            //waveLeftSegments[2] = waveLeftSegment1;
            //waveLeftSegments[3] = waveLeftSegment2;
            //waveLeftSegments[4] = waveLeftSegment1;
            //waveLeftSegments[5] = waveLeftSegment2;
            //AddGesture(GestureType.WaveLeft, waveLeftSegments);

            //IRelativeGestureSegment[] zoomInSegments = new IRelativeGestureSegment[3];
            //zoomInSegments[0] = new ZoomSegment1();
            //zoomInSegments[1] = new ZoomSegment2();
            //zoomInSegments[2] = new ZoomSegment3();
            //AddGesture(GestureType.ZoomIn, zoomInSegments);

            //IRelativeGestureSegment[] zoomOutSegments = new IRelativeGestureSegment[3];
            //zoomOutSegments[0] = new ZoomSegment3();
            //zoomOutSegments[1] = new ZoomSegment2();
            //zoomOutSegments[2] = new ZoomSegment1();
            //AddGesture(GestureType.ZoomOut, zoomOutSegments);

            //IRelativeGestureSegment[] PushSegments = new IRelativeGestureSegment[2];
            //zoomOutSegments[0] = new PushSegment2();
            //zoomOutSegments[1] = new PushSegment1();
            //AddGesture(GestureType.Push, PushSegments);
        }

        /// <summary>
        /// Occurs when [gesture recognised].
        /// </summary>
        public event EventHandler<GestureEventArgs> GestureRecognized;

        /// <summary>
        /// Updates all gestures.
        /// </summary>
        /// <param name="data">The skeleton data.</param>
        public void UpdateAllGestures(Skeleton data)
        {
            foreach (Gesture gesture in this.gestures)
            {
                gesture.UpdateGesture(data);
            }
        }

        /// <summary>
        /// Adds the gesture.
        /// </summary>
        /// <param name="type">The gesture type.</param>
        /// <param name="gestureDefinition">The gesture definition.</param>
        public void AddGesture(GestureType type, IRelativeGestureSegment[] gestureDefinition)
        {
            Gesture gesture = new Gesture(type, gestureDefinition);
            //gesture.GestureRecognized += new EventHandler<GestureEventArgs>(this.Gesture_GestureRecognized);
            gesture.GestureRecognized += OnGestureRecognized;
            this.gestures.Add(gesture);
        }

        /// <summary>
        /// Handles the GestureRecognized event of the g control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KinectSkeltonTracker.GestureEventArgs"/> instance containing the event data.</param>
        //private void Gesture_GestureRecognized(object sender, GestureEventArgs e)
        private void OnGestureRecognized(object sender, GestureEventArgs e)
        {
            if (this.GestureRecognized != null)
            {
                this.GestureRecognized(this, e);
            }

            foreach (Gesture g in this.gestures)
            {
                g.Reset();
            }
        }

        //public void CreateMyForm()
        //{
        //    // Create a new instance of the form.
        //    Form form1 = new Form();

        //    tex.Width = 550;
        //    tex.Height = 550;
        //    tex.Multiline = true;

        //    tex.ScrollBars = ScrollBars.Vertical;
        //    form1.Width = 600;
        //    form1.Height = 600;
        //    form1.Controls.Add(tex);
            


        //    // Display the form as a modal dialog box.
        //    form1.Show();
        //}

    }
}
